<template>
  <div>
    <data-table
      ref="pagingTable"
      :options="options"
      :list-query="listQuery"
      @multi-actions="handleMultiAction"
    >
      <template slot="filter-content">
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd"
        >
          添加
        </el-button>
      </template>
      <template slot="data-columns">
        <el-table-column label="角色ID" prop="id" />

        <el-table-column label="角色名称" prop="roleName" />
      </template>
    </data-table>

    <el-dialog title="添加角色" :visible.sync="dialogVisible" width="400px">
      <el-form :model="formData" label-position="left" label-width="100px">
        <el-form-item label="角色id">
          <el-input v-model="formData.id" />
        </el-form-item>

        <el-form-item label="角色名称">
          <el-input v-model="formData.roleName" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import DataTable from "@/components/DataTable";
import { saveData } from '@/api/sys/role/role'

export default {
  name: "SysRoleList",
  components: { DataTable },
  filters: {
    // 订单状态
    userState(value) {
      const map = {
        0: "正常",
        1: "禁用",
      };
      return map[value];
    },
  },
  data() {
    return {
      dialogVisible: false,
      formData: {},

      listQuery: {
        current: 1,
        size: 10,
        params: {},
      },

      options: {
        // 列表请求URL
        listUrl: "/exam/api/sys/role/paging",
        // 启用禁用
        stateUrl: "/sys/user/state",
      },
    };
  },

  methods: {
    handleAdd() {
      this.formData = {};
      this.dialogVisible = true;
    },

    handleSave() {
      saveData(this.formData).then(() => {
        this.$message({
          type: "success",
          message: "添加角色成功!",
        });
        this.dialogVisible = false;
        this.$refs.pagingTable.getList();
      });
    },

    // 批量操作监听
    handleMultiAction(obj) {
      if (obj.opt === "cancel") {
        this.handleCancelOrder(obj.ids);
      }
    },
  },
};
</script>
